package com.chezhujie.www.controller.api;

import com.chezhujie.www.domain.Czjcommodity;
import com.chezhujie.www.persistence.CzjcommoditytypeMapper;
import com.chezhujie.www.service.*;
import com.xy.core.page.Pagination;
import com.xy.core.utils.ConvertObj;
import com.xy.core.utils.MapManage;
import com.xy.user.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/8/18.
 */
@Controller
@RequestMapping("/api/commodity")
public class CommodityApiController extends BaseController<Czjcommodity>{
    @Autowired
    private CzjcommodityService czjcommodityService;
    @Autowired
    private Czjcommodity3Service czjcommodity3Service;
    @Autowired
    private CzjcommoditytypeService czjcommoditytypeService;
    @Autowired
    private CzjcommoditytypeMapper czjcommoditytypeMapper;
    @Autowired
    private CzjaftersalesService czjaftersalesService;
    @Autowired
    private CzjcommodityimageService czjcommodityimageService;
    @Autowired
    private CzjspecificationService czjspecificationService;

//    @RequestMapping("/typeList.jn")
    public void typeList(HttpServletRequest req, HttpServletResponse resp,
                        ModelMap model) throws Exception {

        try {
            Map<String, Object> param = new HashMap<>();
            List<Map<String, Object>> listtyperesult = czjcommoditytypeMapper.getListByHasCommodity();

            List<Map<String,Object>> listresult = czjcommodity3Service.getCommodity3AllAnd(param);

            Map<String, List<Map<String, Object>>> keylist = MapManage.toGetKeyListMap("typeId", listresult);

            String key = "";
            for (int i = 0; i < listtyperesult.size(); i++) {
                key = (String)listtyperesult.get(i).get("id");

                if(null!=keylist.get(key)){
                    listtyperesult.get(i).put("list", keylist.get(key));
                }else{
                    listtyperesult.get(i).put("list", new ArrayList<>());
                }
            }
            List<Map<String, Object>> list;
            for (int i = listtyperesult.size() - 1; i >= 0; i--) {
                list = (List<Map<String, Object>>)listtyperesult.get(i).get("list");
                if(list.size()==0){
                    listtyperesult.remove(i);
                }
            }
            Map<String, Object> data = new HashMap<>();
            data.put("listSize",listtyperesult.size());
            data.put("list",listtyperesult);
            jsonResult(1,"","",data,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取商品表列表失败!"+e,"",resp);
        }

    }

    /**
     * 商品分类列表
     * @param req
     * @param resp
     * @param model
     * @throws Exception
     */
    @RequestMapping("/typeList.jn")
    public void apiList(
            HttpServletRequest req, HttpServletResponse resp,
            ModelMap model) throws Exception {
        try{

            Pagination pagination = czjcommoditytypeService.getListByHasCommodity();

            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取商品分类列表失败!"+e,"",resp);
        }

    }
    /**
     * 商品列表 通过商品分类查询
     *
     * @param pageNo
     * @param pageSize
     * @param czjcommodity
     * @param req
     * @param resp
     * @param model
     * @throws Exception
     */
    @RequestMapping("/list.jn")
    public void commoditylist(Integer pageNo, Integer pageSize, Czjcommodity czjcommodity, HttpServletRequest req, HttpServletResponse resp,
                              ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            czjcommodity.setSoldOut(0);
            Map<String, Object> params = ConvertObj.toMap(czjcommodity);
            Pagination pagination = czjcommodityService.apilist(pageNo.intValue(), pageSize.intValue(), params, model);
            jsonResult(1, "0", "", pagination, resp);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage() + " #error:" + e);
            jsonResult(0, "99", "获取失败！" + e, "", resp);
        }

    }
    /**
     * 获取商品详情
     * @param commodityId
     * @param req
     * @param resp
     * @param model
     * @throws Exception
     */
    @RequestMapping("/detail.jn")
    public void getCommodityDetail(@RequestParam(required = true) String commodityId,
                                   HttpServletRequest req, HttpServletResponse resp,
                                   ModelMap model) throws Exception {

        try{
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("commodityId", commodityId);
            param.put("orderby","t.orderByno");
            param.put("order","asc");
            List<Map<String, Object>> listaftersales = czjaftersalesService.getApiList(param);
            List<Map<String, Object>> lisstspecification = czjspecificationService.getApiList(param);
            Map<String, Object> param2 = new HashMap<String, Object>();
            param2.put("commodityId", commodityId);
            param2.put("picType", 3);
            List<Map<String, Object>> listimage = czjcommodityimageService.getApiList(param2);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("listaftersales",listaftersales);
            data.put("lisstspecification",lisstspecification);
            data.put("listimage",listimage);
            jsonResult(1,"","",data,resp);
        }catch (Exception e){
            e.printStackTrace();
            log.error("#error:",e);
            jsonResult(0,"99","获取售后服务失败","",resp);
        }

    }
    /**
     * 得到商品首页内容
     * @param commodityId
     * @param req
     * @param resp
     * @param model
     * @throws Exception
     */
    @RequestMapping("/home.jn")
    public void getCommodityHome(@RequestParam(required = true) String commodityId,
                                 HttpServletRequest req, HttpServletResponse resp,
                                 ModelMap model) throws Exception {
        try{
            //商品头像、商品简介、现价、原价、商品名字、商品图片总数
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("commodityId", commodityId);
            Map<String,Object> data = czjcommodityService.queryDetail(map);

            checkCommodity(data);

            Map<String, Object> param = new HashMap<String, Object>();
            param.put("commodityId", commodityId);
            param.put("picType", 0);
            List<Map<String, Object>> map2 = czjcommodityimageService.getApiList(param);
            data.put("imageList",map2);
            //id/imagePath/content/commodityName/originalPrice/presentPrice/soldCount/picCount/commentsCount

            jsonResult(1,"","获取商品详情成功",data,resp);
        }catch (Exception e){
            e.printStackTrace();
            log.error("#error:",e);
            jsonResult(0,"99","获取商品详情失败","",resp);
        }

    }
    private static void checkCommodity(Map<String, Object> data) {
        data.put("status",0);
        if(data.get("soldOut")==null){
            data.put("status", 1);
        }else {
            if(!String.valueOf(data.get("soldOut")).equals("0")){
                data.put("status", 1);
            }
        }

        if(data.get("delFlag")==null){
            data.put("status", 2);
        }else {
            if(!String.valueOf(data.get("delFlag")).equals("0")){
                data.put("status",2);
            }
        }
    }

}
